/*
 * @Description: ex
 * @Author: 亢超
 * @Date: 2024-07-03 20:31:12
 * @LastEditTime: 2024-10-08 22:43:43
 */
/*
 * @Author: 追逐的娃 kangchao_php@163.com
 * @Date: 2024-06-25 14:10:58
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-06-28 09:30:47
 * @FilePath: \vue-project\src\router\index.js
 * @Description: 
 * 
 */
import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    name: 'layout',
    meta: {
      title: '数据统计'
    },
    component: () => import('../views/Layout/Index.vue')
  },
  {
    path: '/login',
    name: 'login',
    meta: {
      title: '登录'
    },
    component: () => import('../views/Common/Login.vue')
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: () => import('../views/Common/404.vue')
  },
]
//动态路由=由于匹配动态菜单
const asyncRoutes = [{
  path: '/',
  name: '/',
  meta: {
    title: '首页'
  },
  component: () => import('@/views/Index/Index.vue')
},
{
  path: '/category/list',
  name: '/category/list',
  meta: {
    title: '分类管理'
  },
  component: () => import('@/views/Shop/Category.vue')
},
{
  path: '/manager/list',
  name: 'manager/list',
  meta: {
    title: '管理员管理'
  },
  component: () => import('@/views/System/Manager.vue')
},
{
  path: '/access/list',
  name: 'access/list',
  meta: {
    title: '菜单管理'
  },
  component: () => import('@/views/System/Access.vue')
}, {
  path: '/role/list',
  name: 'role/list',
  meta: {
    title: '角色管理'
  },
  component: () => import('@/views/System/Role.vue')
},
{
  path: '/setting/base',
  name: '/setting/base',
  meta: {
    title: '系统信息'
  },
  component: () => import('@/views/System/Base.vue')
},
{
  path: '/setting/buy',
  name: '/setting/buy',
  meta: {
    title: '交易信息'
  },
  component: () => import('@/views/System/Buy.vue')
},
{
  path: '/agreement/list',
  name: '/agreement/list',
  meta: {
    title: '协议管理'
  },
  component: () => import('@/views/Other/Agreement.vue')
},
{
  path: '/banner/list',
  name: '/banner/list',
  meta: {
    title: '轮播图管理'
  },
  component: () => import('@/views/Other/Banner.vue')
}]


export const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

//动态添加路由方法
export function addRoutes (menus) {
  //是否有新路由
  let hasNewRoutes = false
  const findAndAddRoutesByMeus = (arr) => {
    arr.forEach(e => {
      let item = asyncRoutes.find(o => o.path == e.frontpath)
      if (item && !router.hasRoute(item.path)) {
        router.addRoute('layout', item)
        hasNewRoutes = true
      }
      if (e.child && e.child.length > 0) {
        findAndAddRoutesByMeus(e.child)
      }
    })
  }
  findAndAddRoutesByMeus(menus)
  return hasNewRoutes
}
